//
//  ViewController.swift
//  SnapKitDemo
//
//  Created by 李煜 on 2023/6/6.
//


import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        
        //控制器根视图设置额外安全区
        self.additionalSafeAreaInsets = UIEdgeInsets(
            top: 8,
            left: 18,
            bottom: 8,
            right: 8
        )
    }
     
    //frame方式设置安全区域
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        debugPrint("viewSafeAreaInsetsDidChange")
        
        if #available(iOS 11.0, *) {
            let yellow = UIView()
            yellow.backgroundColor = .yellow
            yellow.frame = CGRect.init(
                x: self.view.safeAreaInsets.left,
                y: self.view.safeAreaInsets.top,
                width: self.view.frame.size.width-view.safeAreaInsets.left-view.safeAreaInsets.right,
                height: self.view.frame.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom
            )
            view.addSubview(yellow)
            
            
            let blue: UIView = UIView()
            blue.backgroundColor = .blue
            yellow.addSubview(blue)
            blue.frame = CGRect(x: yellow.safeAreaInsets.left-10, y: yellow.safeAreaInsets.top, width: 100, height: 200)
            
            debugPrint("view:" ,view.safeAreaInsets)
            debugPrint("yellow:" ,yellow.safeAreaInsets)
            debugPrint("blue:" ,blue.safeAreaInsets)
        } else {
            // Fallback on earlier versions
        }
    }
    
    
}



#if DEBUG

import SwiftUI

struct ViewControllerRepresentable: UIViewControllerRepresentable {
    typealias UIViewControllerType = ViewController
    
    func makeUIViewController(context: Context) -> UIViewControllerType {
        ViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        
    }
}

struct ViewController_Previews: PreviewProvider {
    static var previews: some View {
        ViewControllerRepresentable()
    }
}

#endif
